//
//  LTFTopicIntrduceView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/21.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFTopicIntrduceView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFBaseModel.h"
#import "LTFTopicTopModel.h"
#import "IsNight.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height


@interface LTFTopicIntrduceView ()


@end


@implementation LTFTopicIntrduceView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        //[self createData];
    }
    return self;
}
- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string retain];
        [self createData];
    }
}
- (void)createView {
    
    self.backView = [[UIView alloc] init];
    [self addSubview:_backView];
    _backView.backgroundColor = [UIColor whiteColor];
    _backView.layer.borderWidth = 1;
    [_backView release];
    
    self.headImageView = [[UIImageView alloc] init];
    //_headImageView.backgroundColor = [UIColor redColor];
    self.headImageView.frame = CGRectMake(6, 6, 50, 50);
    self.headImageView.layer.cornerRadius = 25;
    self.headImageView.layer.masksToBounds = YES;
    [_backView addSubview:_headImageView];
    [_headImageView release];
    
    self.introduceLabel = [[UILabel alloc] init];
    //_introduceLabel.backgroundColor = [UIColor redColor];
    self.introduceLabel.frame = CGRectMake(60, 6, WIDTH - 78, 30);
    [_backView addSubview:_introduceLabel];
    [_headImageView release];
    
    
    self.descriptionLabel = [[UILabel alloc] init];
    //_descriptionLabel.backgroundColor = [UIColor redColor];
    [_backView addSubview:_descriptionLabel];
    [_descriptionLabel release];
    self.descriptionLabel.frame = CGRectMake(60, 40, WIDTH - 78, 10);
    
    self.questionAndReplyLabel = [[UILabel alloc] init];
    //_questionAndReplyLabel.backgroundColor = [UIColor redColor];
    
    [self addSubview:_questionAndReplyLabel];
    [_questionAndReplyLabel release];
    
    
    
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"最新", @"最热"]];
    self.seg.backgroundColor = [UIColor whiteColor];
    self.seg.tintColor = [UIColor redColor];
    self.seg.selectedSegmentIndex = 0;
    self.isclick = NO;
    [self addSubview:_seg];
    [_seg release];
    
    
    if ([IsNight nowIsNight]) {
        _backView.backgroundColor = [IsNight nightColor];
        _introduceLabel.textColor = [IsNight textNightColor];
        _descriptionLabel.textColor = [IsNight textNightColor];
        _questionAndReplyLabel.textColor = [IsNight textNightColor];
        _seg.backgroundColor = [IsNight nightColor];
    }
    else {
        _backView.backgroundColor = [UIColor whiteColor];
        _introduceLabel.textColor = [UIColor blackColor];
        _descriptionLabel.textColor = [UIColor blackColor];
        _questionAndReplyLabel.textColor = [UIColor blackColor];
        _seg.backgroundColor = [UIColor whiteColor];
    }
    
   
}



- (void)createData {
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/qa/%@.html", self.string];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        NSDictionary *data = dictionary[@"data"];
        NSDictionary *expert = data[@"expert"];
        [self.headImageView setImageWithURL:[NSURL URLWithString:expert[@"headpicurl"]] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
        self.introduceLabel.text = [NSString stringWithFormat:@"%@ | %@", expert[@"name"], expert[@"title"]];
        self.descriptionLabel.text = expert[@"description"];
        
        self.questionAndReplyLabel.text = [NSString stringWithFormat:@" %d提问 · %d回复", [expert[@"questionCount"] intValue], [expert[@"answerCount"] intValue]];
        self.descriptionLabel.numberOfLines = 0;
        [self.descriptionLabel sizeToFit];
        
        self.backView.frame = CGRectMake(6, 6, WIDTH - 12, self.descriptionLabel.frame.size.height + 60);
        self.questionAndReplyLabel.frame = CGRectMake(6, self.backView.frame.size.height + 10, WIDTH / 2, 30);
        self.seg.frame = CGRectMake(WIDTH - 112, self.backView.frame.size.height + 10, 100, 30);
        [self.delegate sendHeight:self.backView.frame.size.height + 45];
        NSLog(@"%f", self.backView.frame.size.height + 45);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
